import Vue from 'vue';
import VueRouter from 'vue-router';
import { Toast } from 'vant';


Vue.use(VueRouter)


const Home = () => import('../views/home/Home.vue');
const Categroy = () => import('../views/category/Categry.vue');
const Detail = () => import('../views/detail/Detail.vue');
const Profile = () => import('../views/profile/Profile.vue');
const Edit = () => import('../views/profile/Edit.vue');


const ShopCart = () => import('../views/shopcart/Shopcart.vue');
<<<<<<< HEAD
const Register = () => import('../views/login/Register.vue');
const Login = () => import('../views/login/Login.vue');
=======
const Location = () => import('../views/location/index.vue');
const NewAddress = () => import('../views/location/NewAddress/index.vue');
>>>>>>> location


const routes = [
  {
    path: '',
    redirect: '/home'
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home
  },
  {
    path: '/category',
    component: Categroy
  },
  {
    path: '/detail/:id',
    component: Detail
  },
  {
    path: '/profile',
    component: Profile,
    /* 有istoken: true的时候才会进行 开局判断token */
    meta: {
      istoken: true
    }
  },
  /* 编辑信息页面 */
  {
    path: '/edit',
    component: Edit,
    /* 有istoken: true的时候才会进行 开局判断token */
    meta: {
      istoken: true
    }
  },
  {
    path: '/shopcart',
    component: ShopCart
  },
  {
    path:'/location',
    component:Location
  },
  {
    path:'/location/newaddress',
    component:NewAddress
  },
]


const router = new VueRouter({
  routes
})

// 当没有ajax请求时候 开局判断token 如果没有就跳到登录页面
router.beforeEach((to, from, next) => {
  console.log(to);
  if (!localStorage.getItem('token') && to.meta.istoken == true) {
    router.push('/login')
    // Vue.prototype.$msg.fail('请重新登录')
    Toast.fail('请先登录');
    return
  }
  next()
})



export default router